package com.weishe.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {

	public static Connection conn=null;
	
	public static Connection getConn()throws Exception{
		if(conn==null){
			Class.forName("org.apache.hive.jdbc.HiveDriver");
			conn=  DriverManager.getConnection(
					"jdbc:hive2://hadoop:10000/default", "root", "");
		}
		return conn;
	}
	
	public static void close(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * @param date
	 * @param type h,m
	 */
	public static String getD(Date date,String type){
		SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmm");
		String ds=sdf.format(date);
		if(type.equals("h")){
			return ds.substring(8, 10);
		}else{
			return ds.substring(10);
		}
	}
	
	/**
	 * 
	 * @param date
	 * @param type h,m
	 */
	public static String getDay(Date date,String format){
		SimpleDateFormat sdf =new SimpleDateFormat(format);
		return sdf.format(date);
	}
}
